package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDTO;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exceptions.CustomException;
import com.itheima.reggie.exceptions.CustomExceptionEnum;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: Jetwang
 * @date: 2022/4/11 16:34
 * @description: SetmealServiceImpl
 * @version: 1.0
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    private SetmealMapper setmealMapper;
    private SetmealDishService setmealDishService;

    @Autowired
    public void setSetmealDishService(SetmealDishService setmealDishService) {
        this.setmealDishService = setmealDishService;
    }

    @Autowired
    public void setSetmealMapper(SetmealMapper setmealMapper) {
        this.setmealMapper = setmealMapper;
    }

    @Override
    public void updateStatusByIds(Integer status, List<Long> idList) {
        setmealMapper.updateStatusById(status, idList);
    }

    @Override
    public void saveWithSetmealDish(SetmealDTO setmealDTO) {
        // 添加套餐到setmeal表中
        this.save(setmealDTO);
        // 将套餐菜品添加到setmeal_dish表中
        Long setmealId = setmealDTO.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public void removeWithSetmealDish(Long[] ids) {
        // 判断套餐是否售卖状态
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId, ids);
        lqw.eq(Setmeal::getStatus, 1);
        int count = count(lqw);
        if (count > 0) {
            throw new CustomException(CustomExceptionEnum.SETMEAL_SELLING);
        }
        // 删除套餐关联的菜品
        LambdaQueryWrapper<SetmealDish> lqw01 = new LambdaQueryWrapper<>();
        lqw01.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(lqw01);
        //  删除套餐
        removeByIds(Arrays.asList(ids));
    }

    @Override
    public void updateWithSetmealDish(SetmealDTO setmealDTO) {
        // 更新套餐
        updateById(setmealDTO);
        // 删除原有套餐的菜品
        Long id = setmealDTO.getId();
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, id);
        setmealDishService.remove(lqw);
        // 插入新的套餐菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item -> {
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
}
